package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.NewsPage;
import cn.wolfcode.trip.base.query.NewsPageQueryObject;
import cn.wolfcode.trip.base.service.INewsPageService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/newsPage")
public class NewsPagesController {
    @Autowired
    private INewsPageService newsPageService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") NewsPageQueryObject qo, Model model) {
        qo.setOrderBy("createTime desc");
        model.addAttribute("pageInfo", newsPageService.query(qo));
        return "newsPage/list";
    }


    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(NewsPage entity, MultipartFile img) {
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = UploadUtil.upload(img);
            entity.setCoverUrl(url);
        }
        newsPageService.saveOrUpdate(entity);
        return "redirect:/newsPage/list.do";
    }

    @RequestMapping(value = "/getContentById",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getContentById(Long id) {
        return newsPageService.getContentById(id);
    }
}
